package it.polimi.entities.web;

import java.util.ArrayList;
import java.util.List;

import it.polimi.entitiesDTO.HotelDTO;
import it.polimi.entitiesManagers.HotelMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "HotelBean")
@SessionScoped
public class HotelBean {

	@EJB
	private HotelMgr hmgr;

	private HotelDTO hotelDTO;
	
	private HotelDTO hotelDTO2;
	
	private List<String> cityHotel;
	
	private String city;

	private List<Integer> hotelList;
	
	private int hotelId;
	
	/* ---------------------------
	 * --------CLASS METHODS------
	 * ---------------------------
	 */
	
	public HotelBean() {
		clearAll();
	}
	
	
	private void clearAll(){
		this.hotelDTO = new HotelDTO();
		this.cityHotel= new ArrayList<String>();
		this.city = new String();
		this.hotelList= new ArrayList<Integer>();
		this.hotelId=0;
		this.hotelDTO2= new HotelDTO();
	}
	
	
	public String remove(){
		this.hotelList = hmgr.getAllId();
		if(this.hotelList.contains(this.hotelId))
			{hmgr.remove(this.hotelId);
			clearAll();
			return "/admin/index";
			}
		else return "/admin/errorPage";
	}
	
	public String register() {
		hmgr.save(this.hotelDTO);
		clearAll();
		return "index?faces-redirect=true";
	}
	
	public String update(){
		if(hotelList.contains(hotelId)){
		this.hotelDTO2.setId(hotelId);
		hmgr.update(hotelDTO2);
		clearAll();
		return "index?faces-redirect=true";
		}
		else
		return "/index/errorPage";
	}
	public void fillCityHotel(){
		this.cityHotel=hmgr.getHotelByCity(city);
		this.hotelList=hmgr.getAllId();
	}

	/* ---------------------------
	 * -----GETTERS & SETTERS-----
	 * ---------------------------
	 */
	
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public List<Integer> getHotelList() {
		return hotelList;
	}

	public void setHotelList(List<Integer> hotelList) {
		this.hotelList = hotelList;
	}
	
	public List<String> getCityHotel() {
		return cityHotel;
	}

	public void setCityHotel(List<String> cityHotel) {
		this.cityHotel = cityHotel;
	}

	public HotelDTO getHotelDTO() {
		return hotelDTO;
	}

	public void setHotelDTO(HotelDTO hotelDTO) {
		this.hotelDTO = hotelDTO;
	}


	public int getHotelId() {
		return hotelId;
	}


	public void setHotelId(int hotelId) {
		this.hotelId = hotelId;
	}


	public HotelDTO getHotelDTO2() {
		return hotelDTO2;
	}


	public void setHotelDTO2(HotelDTO hotelDTO2) {
		this.hotelDTO2 = hotelDTO2;
	}
}
